JavaScript syntax
part 29/43 Β· 161.3 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
TypeScript. It evaluates its left-hand operand and, if the result value
is not "nullish" (null or undefined), takes that value as its result;
otherwise, it evaluates the right-hand operand and takes the resulting
value as its result.
In the following example, a will be assigned the value of b if the value
of b is not null or undefined, otherwise it will be assigned 3.
const a = b ?? 3;
Before the nullish coalescing operator, programmers would use the
logical OR operator (||). But where ?? looks specifically for null or
"", 0, NaN, and of course, false.
In the following example, a will be assigned the value of b if the value
of b is truthy, otherwise it will be assigned 3.
const a = b || 3;
Control structures
Compound statements
A pair of curly brackets { } and an enclosed sequence of statements
constitute a compound statement, which can be used wherever a statement
can be used.
If ... else
if (expr) {
//statements;
} else if (expr2) {
//statements;
} else {
//statements;
}
Conditional (ternary) operator
The conditional operator creates an expression that evaluates as one of
two expressions depending on a condition. This is similar to the if
statement that selects one of two statements to execute depending on a
condition. I.e., the conditional operator is to expressions what if is
to statements.
const result = condition ? expression : alternative;
is the same as:
if (condition) {
const result = expression;
} else {
const result = alternative;
}
Unlike the if statement, the conditional operator cannot omit its
"else-branch".
Switch statement
The syntax of the JavaScript switch statement is as follows:
switch (expr) {
case SOMEVALUE:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ